package com.huatai.common.util;

import com.jcraft.jsch.ChannelExec;
import com.jcraft.jsch.JSch;
import com.jcraft.jsch.JSchException;
import com.jcraft.jsch.Session;

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;

public class FileHandleUtil {
	/**
	 * 远程执行命令
	 * @param host
	 * @param user
	 * @param password
	 * @param command
	 * @return
	 */
	public static String fileTranUeFileSSH(String host,String user,String password, String command, String fileId ,int port){
		try {
			JSch jsch = new JSch();
			Session session = jsch.getSession(user, host, port);
			session.setPassword(password);
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect();

			ChannelExec channel = (ChannelExec) session.openChannel("exec");
			channel.setCommand(command);
			channel.setInputStream(null);
			channel.setErrStream(System.err);

			InputStream in = channel.getInputStream();
			channel.connect();

			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			String line;
			channel.disconnect();
			session.disconnect();
			System.out.println("line-------: "+  reader.readLine());
			while ((line = reader.readLine()) == null) {
				System.out.println(line);
				return "fbx文件无效";
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
//		return "/media/test/0B560E66AAC1E38D/WorkSpace/twinpaassplatform_backenduetools/TwinPAASSPlatform_GH/Saved/HotPatcher/"+fileId+"/Linux/"+fileId+"_Linux_001_P.pak";
		return "/media/workspace/twinpaassplatform_backenduetools/TwinPAASSPlatform_GH/Saved/HotPatcher/"+fileId+"/Linux/"+fileId+"_Linux_001_P.pak";
	}

	/**
	 * 远程执行命令
	 * @param host
	 * @param user
	 * @param password
	 * @param getListUrl
	 * @param completeUrl
	 * @return
	 */
	public static void runTargetCommandSSH(String commandPrefix,String host,int port,String user,String password, String getListUrl, String completeUrl) throws JSchException, IOException {
		JSch jsch = new JSch();
		Session session = jsch.getSession(user, host, port);
		session.setPassword(password);
		session.setConfig("StrictHostKeyChecking", "no");
		session.connect();

		String command = "sh "+commandPrefix+" -log  -RenderOffScreen IResourceList="+getListUrl+" IReleaseComplete="+completeUrl;
		ChannelExec channel = (ChannelExec) session.openChannel("exec");
		channel.setCommand(command);
		channel.setInputStream(null);
		channel.setErrStream(System.err);

		InputStream in = channel.getInputStream();
		channel.connect();

		BufferedReader reader = new BufferedReader(new InputStreamReader(in));
		String line;
		while ((line = reader.readLine()) != null) {
			System.out.println(line);
		}
		channel.disconnect();
		session.disconnect();
	}


	public static void main(String[] args) {
		String host = "172.16.4.241"; //服务器IP地址
		String user = "test"; //登陆用户名
		String password = "Bxzh@123"; //登陆密码

		String command = "python3 /media/test/0B560E66AAC1E38D/WorkSpace/twinpaassplatform_backenduetools/import_file.py --file=\"/media/test/0B560E66AAC1E38D/fbx2/卡通小熊.fbx\" --uasset=zyh1 --pkgid=zyh1"; //执行命令

		try {
			JSch jsch = new JSch();
			Session session = jsch.getSession(user, host, 22);
			session.setPassword(password);
			session.setConfig("StrictHostKeyChecking", "no");
			session.connect();

			ChannelExec channel = (ChannelExec) session.openChannel("exec");
			channel.setCommand(command);
			channel.setInputStream(null);
			channel.setErrStream(System.err);

			InputStream in = channel.getInputStream();
			channel.connect();

			BufferedReader reader = new BufferedReader(new InputStreamReader(in));
			String line;
			while ((line = reader.readLine()) != null) {
				System.out.println(line);

			}


			channel.disconnect();
			session.disconnect();
		} catch (Exception e) {
			e.printStackTrace();
		}


//		try {
//			// 要执行的命令
//			String command = "ipconfig";
//
//			// 执行命令
//			Process process = Runtime.getRuntime().exec(command);
//
//			// 获取命令的输出
//			BufferedReader reader = new BufferedReader(new InputStreamReader(process.getInputStream()));
//			String line;
//			while ((line = reader.readLine()) != null) {
//				System.out.println(line);
//			}
//
//			// 等待命令执行完成
//			int exitCode = process.waitFor();
//			System.out.println("Exit code: " + exitCode);
//		} catch (InterruptedException | IOException e) {
//			e.printStackTrace();
//		}
	}


}
